﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using PhoneAppRssReader.Entity;


namespace PhoneAppRssReader.CustomControl
{
    public partial class CustomLongListSelector : INotifyPropertyChanged
    {
        private bool _isProgressBarVisiable = true;
        public bool IsProgressBarVisiable
        {
            get { return _isProgressBarVisiable; }
            set
            {
                _isProgressBarVisiable = value;
                OnPropertyChanged("IsProgressBarVisiable");
            }
        }

        public FeedData FeedData { get; set; }

        private ObservableCollection<FeedItem> _feedItems;
        public List<FeedItem> FeedItems
        {
            get {return _feedItems.OrderByDescending(f => f.PubDate).ToList(); }
        }


        private int _activeIndexItem;
        public int ActiveIndexItem
        {
            get { return _activeIndexItem; }
            set
            {
                _activeIndexItem = value;
                OnPropertyChanged("ActiveIndexItem");
            }
        }



        public CustomLongListSelector()
        {
            InitializeComponent();

            _feedItems = new ObservableCollection<FeedItem>();

            if (FeedData != null) return;
            FeedData = new FeedData();
            FeedData.PropertyChanged += (sender, e) =>
                                             {
                                                 if (e.PropertyName.Equals("FeedItems"))
                                                 {
                                                     _feedItems = FeedData.FeedItems;
                                                     itemControl.ItemsSource = FeedItems.GroupBy(c => "");
                                                     IsProgressBarVisiable = false;

                                                 }
                                                 else if (e.PropertyName.Equals("Error"))
                                                 {
                                                     OnPropertyChanged("Error");
                                                 }
                                             };
            IsProgressBarVisiable = true;
        }

        private void itemControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var list = (LongListSelector)sender;
            if (list.SelectedItem == null)
            {
                return;
            }
            var selectedItem = list.SelectedItem as FeedItem;
            for (var i = 0; i < FeedItems.Count(); i++)
            {
                if (!FeedItems.ToList()[i].Equals(selectedItem)) continue;
                ActiveIndexItem = i;
                return;
            }

        }

        private void UpdateList(object sender, MouseButtonEventArgs e)
        {
            OnPropertyChanged("Request");
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propChanged)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propChanged));
        }

        #endregion
    }
}
